package com.walterlife.javacore;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * ThreadLocalTest
 *
 * @author zhuyuwei
 * @date 2017/5/17
 */
public class ThreadLocalTest {
    public static class Task implements Runnable {
        private ThreadLocal threadLocal = new ThreadLocal();
        private int a = 0;
        private Semaphore semaphore = new Semaphore(1);

        @Override
        public void run() {
//            threadLocal.set((int)(Math.random() * 100D));
//            try {

//                semaphore.acquire();
//                synchronized (ThreadLocalTest.class) {
//                    a = (int) (Math.random() * 100D);
//                }
//                semaphore.release();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {

            }
//            System.out.println(threadLocal.get());
                System.out.println(a);
//        }
//            }
        }
    }

    public static void main(String[] args) {
        Task task = new Task();
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(task);
        executorService.execute(task);
        executorService.shutdown();
    }
}
